package com.live.task;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.live.activity.R;
import com.live.common.Constant;
import com.live.tools.LiveTools;

public class LiveMember extends AsyncTask<Bundle, Integer, String> {
	private Map<String, Object> memberData;

	private String url;

	public SharedPreferences store;

	public LinearLayout liveMemberLayout;

	public String matchId;
	public String id;
	public String team;
	private String matchCategory;

	public LiveMember(LinearLayout liveMemberLayout) {
		this.liveMemberLayout = liveMemberLayout;
	}

	// 此方法在主线程执行,当任务执行之前开始调用此方法，可以在这里显示进度对话框
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		Log.i("Comment", "onPreExecute()  " + Thread.currentThread().getId());
	}

	// 此方法在后台线程执行，完成任务的主要工作，通常需要较长的时间。
	// 在执行过程中可以调用publicProgress(Progress...)来更新任务的进度。

	@Override
	protected String doInBackground(Bundle... params) {
		Log.i("Comment", "doInBackground... " + Thread.currentThread().getId());

		matchId = params[0].getString("matchId");
		id = params[0].getString("id");
		team = params[0].getString("team");
		matchCategory = params[0].getString("matchCategory");

		url = Constant.SITE_URL + "live_member.php?locale=" + LiveTools.getLocaleLanguage() + "&matchId=" + matchId
				+ "&id=" + id + "&team=" + team + "&matchCategory=" + matchCategory;
		memberData = getMember(url);

		publishProgress();
		return null;
	}

	// 此方法在主线程执行，用于显示任务执行的进度
	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);
		Log.i("PageTask", "onProgressUpdate()  " + Thread.currentThread().getId());

		if ("1".equals(matchCategory)) {
			TextView field = (TextView) liveMemberLayout.findViewById(R.id.field);
			field.setText((String) memberData.get("field"));

			TextView three = (TextView) liveMemberLayout.findViewById(R.id.three);
			three.setText((String) memberData.get("three"));

			TextView free = (TextView) liveMemberLayout.findViewById(R.id.free);
			free.setText((String) memberData.get("free"));

			TextView off = (TextView) liveMemberLayout.findViewById(R.id.off);
			off.setText((String) memberData.get("off"));

			TextView def = (TextView) liveMemberLayout.findViewById(R.id.def);
			def.setText((String) memberData.get("def"));

			TextView o = (TextView) liveMemberLayout.findViewById(R.id.o);
			o.setText((String) memberData.get("o"));

			TextView ass = (TextView) liveMemberLayout.findViewById(R.id.ass);
			ass.setText((String) memberData.get("ass"));

			TextView fouls = (TextView) liveMemberLayout.findViewById(R.id.fouls);
			fouls.setText((String) memberData.get("fouls"));

			TextView ste = (TextView) liveMemberLayout.findViewById(R.id.ste);
			ste.setText((String) memberData.get("ste"));

			TextView turn = (TextView) liveMemberLayout.findViewById(R.id.turn);
			turn.setText((String) memberData.get("turn"));

			TextView blo = (TextView) liveMemberLayout.findViewById(R.id.blo);
			blo.setText((String) memberData.get("blo"));

			TextView points = (TextView) liveMemberLayout.findViewById(R.id.points);
			points.setText((String) memberData.get("points"));
		} else if ("2".equals(matchCategory)) {
			if (!"null".equals(String.valueOf(memberData.get("c_q")))) {
				TextView c_q = (TextView) liveMemberLayout.findViewById(R.id.c_q);
				c_q.setText((String) memberData.get("c_q"));

				TextView q_d = (TextView) liveMemberLayout.findViewById(R.id.q_d);
				q_d.setText((String) memberData.get("q_d"));

				TextView t_q = (TextView) liveMemberLayout.findViewById(R.id.t_q);
				t_q.setText((String) memberData.get("t_q"));

				TextView l_j = (TextView) liveMemberLayout.findViewById(R.id.l_j);
				l_j.setText((String) memberData.get("l_j"));

				TextView g_r = (TextView) liveMemberLayout.findViewById(R.id.g_r);
				g_r.setText((String) memberData.get("g_r"));

				TextView f_g = (TextView) liveMemberLayout.findViewById(R.id.f_g);
				f_g.setText((String) memberData.get("f_g"));

				TextView b_f = (TextView) liveMemberLayout.findViewById(R.id.b_f);
				b_f.setText((String) memberData.get("b_f"));

				TextView y_w = (TextView) liveMemberLayout.findViewById(R.id.y_w);
				y_w.setText((String) memberData.get("y_w"));

				TextView z_g = (TextView) liveMemberLayout.findViewById(R.id.z_g);
				z_g.setText((String) memberData.get("z_g"));

				TextView s_m = (TextView) liveMemberLayout.findViewById(R.id.s_m);
				s_m.setText((String) memberData.get("s_m"));

				TextView p_j = (TextView) liveMemberLayout.findViewById(R.id.p_j);
				p_j.setText((String) memberData.get("p_j"));

				TextView h_h_p = (TextView) liveMemberLayout.findViewById(R.id.h_h_p);
				h_h_p.setText((String) memberData.get("h_h_p"));

				TextView j_q_s = (TextView) liveMemberLayout.findViewById(R.id.j_q_s);
				j_q_s.setText((String) memberData.get("j_q_s"));
			}
		}
	}

	// 此方法在主线程执行，任务执行的结果作为此方法的参数返回。
	@Override
	protected void onPostExecute(String result) {
		// tv.setText(result);
		Log.i("PageTask", "onPostExecute()  " + Thread.currentThread().getId());
	}

	private Map<String, Object> getMember(String url) {
		Map<String, Object> data = new HashMap<String, Object>();
		HttpClient client = new DefaultHttpClient();
		// 提拱默认的HttpClient实现
		HttpPost request;
		try {
			request = new HttpPost(new URI(url));
			HttpResponse response = client.execute(request);
			// 判断请求是否成功
			if (response.getStatusLine().getStatusCode() == 200) { // 200表示请求成功
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					String out = EntityUtils.toString(entity);
					try {
						String result = new JSONObject(out).getString("result");
						if (Constant.Y.equals(result)) {
							JSONArray member = new JSONObject(out).getJSONArray("results");
							for (int i = 0; i < member.length(); i++) {
								if ("1".equals(matchCategory)) {
									data.put("field", member.getJSONObject(i).get("field"));
									data.put("three", member.getJSONObject(i).get("three"));
									data.put("free", member.getJSONObject(i).get("free"));
									data.put("off", member.getJSONObject(i).get("off"));
									data.put("def", member.getJSONObject(i).get("def"));
									data.put("o", member.getJSONObject(i).get("o"));
									data.put("ass", member.getJSONObject(i).get("ass"));
									data.put("fouls", member.getJSONObject(i).get("fouls"));
									data.put("ste", member.getJSONObject(i).get("ste"));
									data.put("turn", member.getJSONObject(i).get("turn"));
									data.put("blo", member.getJSONObject(i).get("blo"));
									data.put("points", member.getJSONObject(i).get("points"));
								} else if ("2".equals(matchCategory)) {
									data.put("c_q", member.getJSONObject(i).get("c_q"));
									data.put("q_d", member.getJSONObject(i).get("q_d"));
									data.put("t_q", member.getJSONObject(i).get("t_q"));
									data.put("l_j", member.getJSONObject(i).get("l_j"));
									data.put("g_r", member.getJSONObject(i).get("g_r"));
									data.put("f_g", member.getJSONObject(i).get("f_g"));
									data.put("b_f", member.getJSONObject(i).get("b_f"));
									data.put("y_w", member.getJSONObject(i).get("y_w"));
									data.put("z_g", member.getJSONObject(i).get("z_g"));
									data.put("s_m", member.getJSONObject(i).get("s_m"));
									data.put("p_j", member.getJSONObject(i).get("p_j"));
									data.put("h_h_p", member.getJSONObject(i).get("h_h_p"));
									data.put("j_q_s", member.getJSONObject(i).get("j_q_s"));
								}
							}
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}

}